"""
元数据模型定义
"""
import os
import logging
import threading
from typing import Dict, Optional, Any
from dataclasses import dataclass
from datetime import datetime

# 配置日志
logging.basicConfig(
    format='[%(process)d][%(thread)d][%(filename)s#%(lineno)d] %(levelname)s: %(message)s',
    level=logging.DEBUG
)
logger = logging.getLogger(__name__)

@dataclass
class PageMetadata:
    """网页元数据模型"""
    url: str
    title: Optional[str] = None
    description: Optional[str] = None
    keywords: Optional[str] = None
    author: Optional[str] = None
    publish_date: Optional[str] = None
    domain: Optional[str] = None
    extracted_at: datetime = datetime.now()
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'PageMetadata':
        """从字典创建元数据对象"""
        process_id = os.getpid()
        thread_id = threading.get_ident()
        logger.debug(f"从字典创建元数据对象")
        
        return cls(
            url=data.get('url', ''),
            title=data.get('title'),
            description=data.get('description'),
            keywords=data.get('keywords'),
            author=data.get('author'),
            publish_date=data.get('publish_date'),
            domain=data.get('domain'),
            extracted_at=datetime.now()
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        process_id = os.getpid()
        thread_id = threading.get_ident()
        logger.debug(f"将元数据对象转换为字典")
        
        return {
            'url': self.url,
            'title': self.title,
            'description': self.description,
            'keywords': self.keywords,
            'author': self.author,
            'publish_date': self.publish_date,
            'domain': self.domain,
            'extracted_at': self.extracted_at.isoformat()
        } 